package work02.atm;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class MockDbConnection implements IDBConnection{
	private HashMap<String,Account> mdb;
	
	public MockDbConnection(){
		mdb= new HashMap<>();
		Account a=new Account("1","11111",300);
		Account b=new Account("2","22222",2000);
		mdb.put(a.getCardNo(),a);
		mdb.put(b.getCardNo(),b);
	}
	
	public Account getAccount(String cardNo){
		Account account = null;
		Iterator<Map.Entry<String,Account>> entries = mdb.entrySet().iterator();  
		while (entries.hasNext()) {  
		  
		    Map.Entry<String, Account> entry = entries.next(); 
		    if(entry.getKey().equals(cardNo)){
		    	account = entry.getValue();
		    	break;
		    }
		}  
		return account;		
	}
	public void updateAccount(Account a){
		Iterator<Map.Entry<String,Account>> entries = mdb.entrySet().iterator();
		while (entries.hasNext()) { 
		    Map.Entry<String, Account> entry = entries.next(); 
		    if(entry.getValue().getCardNo().equals(a.getCardNo())){
		    	mdb.put(entry.getKey(),a);
		    	break;
		    }
		}  
	}
	public void addAccount(Account a){
		mdb.put(a.getCardNo(),a);
	}
}
